﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.Helpers;
using SYcwpb.ViewModels;
using System.Collections.Specialized;
using SYcwpb.Controls;
using System.Data;
using SYcwpb.Models;
using SYcwpb.ViewModels.DataProvider;
using SYcwpb.WinForm;
using Microsoft.Windows.Controls;
using System.Threading;

namespace SYcwpb.Views
{
    /// <summary>
    /// CrossingRoadEdit.xaml 的交互逻辑
    /// </summary>
    public partial class CrossingRoadView : Window
    {
        public static CrossingRoadView _view = new CrossingRoadView();
        private static CrossingRoadViewModel _viewModel ;

        public List<ComboBox> BianCArray_First = new List<ComboBox>();    //便乘到第一个站的车次号控件
        public List<ComboBox> BianCArray_Last = new List<ComboBox>();//便乘到最后一个站的车次号控件

        public List<DataGrid> ClassList = new List<DataGrid>();

        ///// <summary>
        ///// 早/白班交接班时间
        ///// </summary>
        //public static string str_ChangeTime_ZB = "07:58:00";
        //public int int_ChangeTime_ZB = GlobalVariables.FormatTimeToInt(str_ChangeTime_ZB);

        string configFileName = "CrossingRoadConfig" + CONSTDATA.UserModel.Line + ".ini";
        public IniFileHelper iniFileHelper = null;

        private CrossingRoadView()
        {
            InitializeComponent();

            iniFileHelper = new IniFileHelper(configFileName);

            ClassList.Add(dg_zb);
            ClassList.Add(dg_bb);
            ClassList.Add(dg_yb);
        }

        public static CrossingRoadView GetInstance()
        {
            if (null == _view || WindowHelper.IsDisposed(_view))
            {
                _view = new CrossingRoadView();

                _viewModel = new CrossingRoadViewModel();

                _view.DataContext = _viewModel;

                _viewModel.loadWarnColorConfig();
            }
            return _view;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cb_tableName.SelectionChanged += new SelectionChangedEventHandler(cb_tableName_SelectionChanged);
            //cb_BianC_Last.SelectionChanged += new SelectionChangedEventHandler(cb_BianC_Last_SelectionChanged);
            //cb_BianC_First.SelectionChanged += new SelectionChangedEventHandler(cb_BianC_First_SelectionChanged);
        }
        private CrossingRoadParameter InitTimeInfo1(string TableName)
        {
            
            CrossingRoadParameter parameters = _viewModel.LoadParameters(TableName);
            if (parameters == null)
            {
                InitTimeInfo(TableName);
                return null;
            }
            try
            {

                string[] tempArr = GlobalVariables.FormatTime(parameters.int_ChangeTime_ZB).Split(':');
                stp_zb.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_zb.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_zb.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = GlobalVariables.FormatTime(parameters.int_ChangeTime_BB).Split(':');
                stp_by.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_by.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_by.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = GlobalVariables.FormatTime(parameters.int_ZB_OutTime).Split(':');
                stp_outZB.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_outZB.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_outZB.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = GlobalVariables.FormatTime(parameters.int_ZB_BackTime).Split(':');
                stp_backZB.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_backZB.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_backZB.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = GlobalVariables.FormatTime(parameters.int_BB_OutTime).Split(':');
                stp_outBB.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_outBB.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_outBB.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = GlobalVariables.FormatTime(parameters.int_LunchTime).Split(':');
                lunch_time.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                lunch_time.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                lunch_time.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = GlobalVariables.FormatTime(parameters.int_SuperTime).Split(':');
                dinner_time.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                dinner_time.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                dinner_time.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                _viewModel.RestTime = parameters.restTime + "";
                _viewModel.MealRestTime = parameters.restTime + "";
                _viewModel.LunchCrossRoadCount = parameters.lunchCrossRoadCount.ToString();
                _viewModel.SupperCrossRoadCount = parameters.supperCrossRoadCount.ToString();
                _viewModel.GarageAhead = parameters.GarageAhead.ToString();
                _viewModel.GarageBCAhead = parameters.GarageBCAhead.ToString();
                _viewModel.NormalAhead = parameters.NormalAhead.ToString();
                _viewModel.NormalDelay = parameters.NormalDelay.ToString();
                _viewModel.GarageDelay = parameters.GarageDelay.ToString();
                _viewModel.BcGarageDelay = parameters.BcGarageDelay.ToString();
                _viewModel.PhoneOutTrain = parameters.PhoneOutTrain;
                _viewModel.NightEmptyTrain = parameters.NightEmptyTrain;

                return parameters;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 旧的参数获取方式，已注释
        /// </summary>
        /// <param name="TableName"></param>
        private void InitTimeInfo(string TableName)
        {
            NameValueCollection values = new NameValueCollection();
            iniFileHelper.ReadSectionValues(TableName, values);
            if (values.Count < 10)
                return;
            try
            {
                string changeZb = values[0];
                string changeby = values[1];
                string zbouttime = values[2];
                string zbbacktime = values[3];
                string bbouttime = values[4];
                string lunchtime = values[5];
                string dinertime = values[6];
                string lunchNum = values[7];
                string supperNum = values[8];
                string restTime = values[9];
                string mealRestTime = values[10];
                string garageAhead = values[11];
                string garageBCAhead = values[12];
                string normalAhead = values[13];

                string[] tempArr = changeZb.Split(':');
                stp_zb.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_zb.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_zb.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = changeby.Split(':');
                stp_by.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_by.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_by.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = zbouttime.Split(':');
                stp_outZB.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_outZB.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_outZB.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = zbbacktime.Split(':');
                stp_backZB.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_backZB.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_backZB.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = bbouttime.Split(':');
                stp_outBB.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                stp_outBB.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                stp_outBB.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = lunchtime.Split(':');
                lunch_time.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                lunch_time.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                lunch_time.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                tempArr = dinertime.Split(':');
                dinner_time.InitScrollBarHour = Convert.ToUInt32(tempArr[0]);
                dinner_time.InitScrollBarMin = Convert.ToUInt32(tempArr[1]);
                dinner_time.InitScrollBarSec = Convert.ToUInt32(tempArr[2]);

                _viewModel.RestTime = restTime;
                _viewModel.MealRestTime = mealRestTime;
                _viewModel.LunchCrossRoadCount = lunchNum;
                _viewModel.SupperCrossRoadCount = supperNum;
                _viewModel.GarageAhead = garageAhead;
                _viewModel.GarageBCAhead = garageBCAhead;
                _viewModel.NormalAhead = normalAhead;
            }
            catch (System.Exception ex)
            {

            }
        }
        void InitParametersPanel(CrossingRoadParameter parameters)
        {

            int stp_zbT = (int)this.stp_zb.GetTimeSecond();

            int stp_zb_outT = (int)this.stp_outZB.GetTimeSecond();

            int zbTrainT = stp_zbT > stp_zb_outT ? stp_zbT : stp_zb_outT;

            _viewModel.InitTrainList(zbTrainT.ToString(), this.stp_by.GetTimeSecond().ToString());
            //cb_BianC_Last.Text = "0";
            //cb_BianC_First.Text = "0";
            _viewModel.UnNightBC_OutCollections.Clear();
            _viewModel.UnMorningBCCollections.Clear();
            AddComBoxaToSta(stk_bc_last, 0, "Last");
            AddComBoxaToSta(stk_bc_first, 0, "First");
            _viewModel.Is_NightMeal_BC = false;
            _viewModel.SetMealCrossRoalCollections.Clear();
            _viewModel.EarlyBackGarageCollections.Clear();

            if (parameters != null)
            {
                _viewModel.PhoneOutTrain = parameters.PhoneOutTrain;
                _viewModel.NightEmptyTrain = parameters.NightEmptyTrain;

                //_viewModel.EarlyBackGarageCollections.Clear();
                //_viewModel.UnMorningBCCollections.Clear();
                //_viewModel.UnNightBC_OutCollections.Clear();
                //_viewModel.SetMealCrossRoalCollections.Clear();

                if (parameters.bc_first_arr != null && parameters.bc_first_arr.Count > 0)
                {
                    //cb_BianC_First.SelectedIndex = parameters.bc_first_arr.Count;
                    BianCArray_First = new List<ComboBox>(parameters.bc_first_arr.Count);
                    AddComBoxaToSta(stk_bc_first, parameters.bc_first_arr.Count, "First");
                    for (int i = 0; i < parameters.bc_first_arr.Count; i++)
                    {
                        BianCArray_First[i].Text = parameters.bc_first_arr[i].ToString();
                    }

                }
                else
                {
                    AddComBoxaToSta(stk_bc_first, 0, "First");
                }
                if (parameters.bc_last_arr != null && parameters.bc_last_arr.Count > 0)
                {
                    //cb_BianC_Last.SelectedIndex = parameters.bc_last_arr.Count;
                    BianCArray_Last = new List<ComboBox>(parameters.bc_last_arr.Count);
                    AddComBoxaToSta(stk_bc_last, parameters.bc_last_arr.Count, "Last");
                    for (int i = 0; i < parameters.bc_last_arr.Count; i++)
                    {
                        BianCArray_Last[i].Text = parameters.bc_last_arr[i].ToString();
                    }
                }
                else
                {
                    AddComBoxaToSta(stk_bc_last, 0, "Last");
                }

                if (parameters.UnMorningBcCollection != null && parameters.UnMorningBcCollection.Count > 0)
                {
                    foreach (var key in parameters.UnMorningBcCollection.Keys)
                    {
                        string text = null;
                        switch (parameters.UnMorningBcCollection[key])
                        {
                            case ClassType.Day:
                                text = "白班";
                                break;
                            case ClassType.Night:
                                text = "夜班";
                                break;
                            default:
                                break;
                        }
                        UnMoringBCModel item = new UnMoringBCModel() { SelectedClass = text, TrainID = key };
                        _viewModel.UnMorningBCCollections.Add(item);
                    }
                }
                else
                {

                }

                if (parameters.UnNightBCOutCollection != null && parameters.UnNightBCOutCollection.Count > 0)
                {
                    foreach (var item in parameters.UnNightBCOutCollection)
                    {
                        UnNightBCOutModel i = new UnNightBCOutModel() { TrainID = item.ToString() };
                        _viewModel.UnNightBC_OutCollections.Add(i);
                    }
                }
                else
                {

                }

                if (parameters.EarlyBackGarageCollections != null && parameters.EarlyBackGarageCollections.Count > 0)
                {
                    foreach (var item in parameters.EarlyBackGarageCollections)
                    {
                        item.NightBackTrainCollections = _viewModel.NightBackTrains;
                        foreach (var station in item.StationCollctions)
                        {
                            if (station.StationName == item.StationSelected.StationName)
                            {
                                item.StationSelected = station;
                                break;
                            }
                        }
                        _viewModel.EarlyBackGarageCollections.Add(item);

                    }
                }
                else
                {

                }

                if (parameters.SetMealCrossRoadCollection != null && parameters.SetMealCrossRoadCollection.Count > 0)
                {
                    foreach (var item in parameters.SetMealCrossRoadCollection)
                    {
                        _viewModel.SetMealCrossRoalCollections.Add(item);
                    }
                }
                else
                {

                }

                if (parameters.Is_NightMeal_BC)
                    _viewModel.Is_NightMeal_BC = true;
                else
                    _viewModel.Is_NightMeal_BC = false;

            }
            else
            {

            }
        }
        void cb_tableName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!CheckTableNameSelected())
                return;
            cbVersion.SelectedIndex = -1;
            var parameters = this.InitTimeInfo1(_viewModel.TimeTable.Name);

            //cb_BianC_Last.Text = "0";
            //cb_BianC_First.Text = "0";
            List<string> list=_viewModel.InitVersion();
            cbVersion.ItemsSource = list;
            //_viewModel.CheckIsCrossingRoadIn();
            BianCArray_First = new List<ComboBox>();    //便乘到第一个站的车次号控件
            BianCArray_Last = new List<ComboBox>();//便乘到最后一个站的车次号控件

            //if (_viewModel._isCrossRoadIn)  //如果该时刻表有可用的交路表，则加载出来
            //{
            //    _viewModel.LoadExistCrossingRoad();
            //}
            //InitParametersPanel(parameters);

        }

        void cb_BianC_First_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (string.IsNullOrEmpty(_viewModel.TimeTable.ID))
            //{
            //    MessageBoxHelper.ShowTips("请先选择时刻表！");
            //    cb_BianC_First.Text = "0";
            //    return;
            //}

            //BianCArray_First = new List<ComboBox>(cb_BianC_First.SelectedIndex);

            //AddComBoxaToSta(stk_bc_first, cb_BianC_First.SelectedIndex, "First");
        }

        void cb_BianC_First_Add(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_viewModel.TimeTable.ID))
            {
                MessageBoxHelper.ShowTips("请先选择时刻表！");
                //cb_BianC_First.Text = "0";
                return;
            }

            ComboBox cb = InitNewCombox(BianCArray_First.Count, "First");
            stk_bc_first.Children.Add(cb);
            BianCArray_First.Add(cb);
        }

        private void cb_BianC_First_Del(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_viewModel.TimeTable.ID))
            {

                MessageBoxHelper.ShowTips("请先选择时刻表！");
                //cb_BianC_Last.Text = "0";
                return;
            }
            if (BianCArray_First.Count <= 0)
            {
                MessageBoxHelper.ShowTips("不能少于0！");
                return;
            }
            stk_bc_first.Children.Remove(BianCArray_First[BianCArray_First.Count - 1]);
            BianCArray_First.Remove(BianCArray_First[BianCArray_First.Count - 1]);
        }

        void cb_BianC_Last_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (string.IsNullOrEmpty(_viewModel.TimeTable.ID))
            //{
                
            //    MessageBoxHelper.ShowTips("请先选择时刻表！");
            //    cb_BianC_Last.Text = "0";
            //    return;
            //}
            //BianCArray_Last = new List<ComboBox>(cb_BianC_Last.SelectedIndex);

            //AddComBoxaToSta(stk_bc_last, cb_BianC_Last.SelectedIndex, "Last");
        }

        void cb_BianC_Last_Add(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_viewModel.TimeTable.ID))
            {

                MessageBoxHelper.ShowTips("请先选择时刻表！");
                //cb_BianC_Last.Text = "0";
                return;
            }
            ComboBox cb = InitNewCombox(BianCArray_Last.Count, "Last");
            stk_bc_last.Children.Add(cb);
            BianCArray_Last.Add(cb);
            //AddComBoxaToSta(stk_bc_last, cb_BianC_Last.SelectedIndex, "Last");
        }

        private void cb_BianC_Last_Del(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_viewModel.TimeTable.ID))
            {

                MessageBoxHelper.ShowTips("请先选择时刻表！");
                //cb_BianC_Last.Text = "0";
                return;
            }
            if (BianCArray_Last.Count <= 0)
            {
                MessageBoxHelper.ShowTips("不能少于0！");
                return;
            }
            stk_bc_last.Children.Remove(BianCArray_Last[BianCArray_Last.Count - 1]);
            BianCArray_Last.Remove(BianCArray_Last[BianCArray_Last.Count - 1]);
        }

        private void AddComBoxaToSta(WrapPanel stkPanel, int num, string cbName)
        {
            stkPanel.Children.RemoveRange(0, stkPanel.Children.Count);

            for (int i = 0; i < num; i++ )
            {
                ComboBox cb = InitNewCombox(i, cbName);
                stkPanel.Children.Add(cb);
                if (cbName == "Last") { BianCArray_Last.Add(cb); }
                else { BianCArray_First.Add(cb); }
            }
        }

        private ComboBox InitNewCombox(int index, string cbName)
        {
            ComboBox newCb = new ComboBox();
            newCb.Name = cbName + index.ToString();
            newCb.ItemsSource = _viewModel.ZBTrainList;
            return newCb;
        }

        public bool CheckBianCTrain()
        {
            bool result = false;
            for (int i = 0; i < BianCArray_First.Count; i++)
            {
                if (BianCArray_First[i].SelectedIndex < 0) { return result; }
            }
            for (int j = 0; j < BianCArray_Last.Count; j++)
            {
                if (BianCArray_Last[j].SelectedIndex < 0) { return result; }
            }

            result = true;
            return result;
        }

        public bool CheckTableNameSelected()
        {
            return cb_tableName.SelectedIndex > -1;
        }

        public CrossingRoadObject GetSelectedRow(string cls)
        {
            CrossingRoadObject row = null;

            switch (cls)
            {
                case"ZB":
                    row = dg_zb.SelectedItem as CrossingRoadObject;
                    break;
                case "BB":
                    row = dg_bb.SelectedItem as CrossingRoadObject;
                    break;
                case "YB":
                    row = dg_yb.SelectedItem as CrossingRoadObject;
                    break;
            }

            return row;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!_viewModel.IsExporting)  
            {
                MessageBoxHelper.ShowTips("无法关闭窗口：请等候导出完成后重试！");
                e.Cancel = true;
            }
            
            try
            {
                if (_viewModel.ExportExcelThread != null)
                    if (_viewModel.ExportExcelThread.IsAlive)
                        _viewModel.ExportExcelThread.Abort();
            }
            catch (System.Exception ex)
            {
            }
            finally
            {
                _viewModel.ExportExcelThread = null;
            }
        }

        private void btn_Remove_EarlyBackGarageClick(object sender, RoutedEventArgs e)
        {
            EarlyBackGarageModel item = (sender as Button).DataContext as EarlyBackGarageModel;

            _viewModel.EarlyBackGarageCollections.Remove(item);
        }

        private void btn_Remove_UnMorningBC_Click(object sender, RoutedEventArgs e)
        {
            UnMoringBCModel item = (sender as Button).DataContext as UnMoringBCModel;

            _viewModel.UnMorningBCCollections.Remove(item);
        }
        private void btn_Remove_PhoneAttendOut_Click(object sender, RoutedEventArgs e)
        {
            TrainIdModel item = (sender as Button).DataContext as TrainIdModel;

            _viewModel.PhoneAttendOutCollections.Remove(item);
        }
        private void btn_Remove_UnNightBC_Out_Click(object sender, RoutedEventArgs e)
        {
            UnNightBCOutModel item = (sender as Button).DataContext as UnNightBCOutModel;

            _viewModel.UnNightBC_OutCollections.Remove(item);
        }
        private void btn_Remove_Unplan_Click(object sender, RoutedEventArgs e)
        {
            TrainIdModel item = (sender as Button).DataContext as TrainIdModel;

            _viewModel.UnplanCollections.Remove(item);
        }
        private void btn_Remove_SetMealClick(object sender, RoutedEventArgs e)
        {
            SetMealCrossRoadModel item = (sender as Button).DataContext as SetMealCrossRoadModel;

            _viewModel.SetMealCrossRoalCollections.Remove(item);
        }

        private void DataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
        {
            Microsoft.Windows.Controls.DataGrid datagrid = sender as Microsoft.Windows.Controls.DataGrid;

            int i = e.Row.GetIndex();

            if (null != datagrid)
            {
                CrossingRoadObjects roadCollections = datagrid.ItemsSource as CrossingRoadObjects;

                roadCollections.OnCalculateAverage();

                CrossingRoadObject road = roadCollections[i];
                
                if (null != road)
                {
                    double whour = 0;
                    double miles = 0;

                    if (datagrid.Name == "dg_zb")
                    {
                        if (double.TryParse(road.Miles, out miles))
                            if (miles > _viewModel._warnColorConfig.WarnMilesZB)
                                e.Row.Background = new SolidColorBrush(_viewModel._warnColorConfig.WarnColorMiles);
                            else if (double.TryParse(road.Whours, out whour))
                                if (whour > _viewModel._warnColorConfig.WarnWhourZB)
                                    e.Row.Background = new SolidColorBrush(_viewModel._warnColorConfig.WarnColorWhours);
                    }
                    else if (datagrid.Name == "dg_bb")
                    {
                        if (double.TryParse(road.Miles, out miles))
                            if (miles > _viewModel._warnColorConfig.WarnMilesBB)
                                e.Row.Background = new SolidColorBrush(_viewModel._warnColorConfig.WarnColorMiles);
                            else if (double.TryParse(road.Whours, out whour))
                                if (whour > _viewModel._warnColorConfig.WarnWhourBB)
                                    e.Row.Background = new SolidColorBrush(_viewModel._warnColorConfig.WarnColorWhours);
                    }
                    else if (datagrid.Name == "dg_yb")
                    {
                        if (double.TryParse(road.Miles, out miles))
                            if (miles > _viewModel._warnColorConfig.WarnMilesYB)
                                e.Row.Background = new SolidColorBrush(_viewModel._warnColorConfig.WarnColorMiles);
                            else if (double.TryParse(road.Whours, out whour))
                                if (whour > _viewModel._warnColorConfig.WarnMilesYB)
                                    e.Row.Background = new SolidColorBrush(_viewModel._warnColorConfig.WarnColorWhours);
                    }
                }
            }
        }
        private void cbVersion_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                _viewModel.LastModifiedTime = null;
                btnGen.IsEnabled = true;
                _viewModel.IsDraftVersion = false;
                if (cbVersion.SelectedIndex == -1)
                {
                    btnGen.IsEnabled = false;
                    _viewModel.ClearCrossingRoad();
                }
                else if (cbVersion.SelectedIndex == 0)
                {
                    _viewModel.ClearCrossingRoad();
                    _viewModel.CheckIsCrossingRoadIn();

                    if (_viewModel._isCrossRoadIn)  //如果该时刻表有可用的交路表，则加载出来
                    {
                        _viewModel.LoadExistCrossingRoad();
                    }
                }
                else if (cbVersion.SelectedIndex == cbVersion.Items.Count - 1)
                {
                    _viewModel.ClearCrossingRoad();
                }
                else
                {
                    _viewModel.ClearCrossingRoad();
                    string draftName = cbVersion.SelectedItem.ToString();
                    _viewModel.LoadCrossingRoadDraft(draftName);
                    _viewModel.IsDraftVersion = true;
                }
                
                var parameters = this.InitTimeInfo1(_viewModel.TimeTable.Name);
                InitParametersPanel(parameters);
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void btnDelDraft_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                string draftName = cbVersion.SelectedItem.ToString();
                _viewModel.DeleteDraft(draftName);

                _viewModel.ClearCrossingRoad();
                cbVersion.SelectedIndex = -1;
                btnDelDraft.IsEnabled = false;
                List<string> list = _viewModel.InitVersion();
                cbVersion.ItemsSource = list;
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void dg_class_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var datagrid=sender as DataGrid;
                if (datagrid != null && datagrid.SelectedIndex!= -1)
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.Control)
                    {
                        foreach (var item in ClassList)
                        {
                            if (item != datagrid)
                            {
                                item.SelectedIndex = -1;
                            }
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void CSVCBB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CSVCBB.SelectedIndex >= 0)
            {
                _viewModel.GetTableList(CSVCBB.SelectedIndex);
                cb_tableName.ItemsSource = _viewModel.TimeTableCollections;
            }
        }



        //private void ZB_MenuItem_Click(object sender, RoutedEventArgs e)
        //{
        //    MenuItem item = sender as MenuItem;
        //    switch ((string)item.Header)
        //    {
        //        case "新增交路":
        //            CrossiRoadEditor.GetInstance().ShowDialog();
        //            break;
        //        case "编辑选中交路":
        //            CrossiRoadEditor.GetInstance().ShowDialog();
        //            break;
        //        case "删除选中交路":
        //            break;
        //    }
        //}

    }
}
